﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Models.Sellers;
using S4Store.Service;
using S4Store.Domain;
using S4Store.Models;
using S4Store.Domain.NetMarketingKpi;
using S4Store.Models.NetMarketing;

namespace S4Store.Controllers
{
    public class NetMarketingKpiController : ReportBaseController
    {
        public NetMarketingKpiService NetMarketingKpiService { get; set; }

        #region Media
        public ActionResult MediaIndex()
        {
            BaseReportModel model = new BaseReportModel();
            InjectReportModel(model);

            return View(model);
        }

        public ActionResult MediaQuery()
        {
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                var data = NetMarketingKpiService.GetMedias(Store.Id).ToList();
                return View(data);
            }
        }


        public ActionResult AddMedia()
        {
                return View();
        }

        [HttpPost]
        public JsonResult AddMedia(NetMarketingMedia model)
        {
            model.StoreId = Store.Id;
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                NetMarketingKpiService.AddNetMarketingMedia(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

        public ActionResult ModifyMedia(int id)
        {
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                var media = NetMarketingKpiService.GetMediaQuery().FirstOrDefault(x => x.Id == id);
                return View(media);
            }

        }

        [HttpPost]
        public JsonResult ModifyMediaBack(NetMarketingMedia model)
        {
            model.StoreId = Store.Id;
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                NetMarketingKpiService.ModifyNetMarketingMedia(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }

        }


        [HttpPost]
        public JsonResult DeleteMedia(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要删除的项");
            }
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                foreach (var id in ids)
                {
                    NetMarketingKpiService.DeleteNetMarketingMedia(id);
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "删除成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpPost]
        public JsonResult Close(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要下线的媒体");
            }
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                foreach (var id in ids)
                {
                    var model = NetMarketingKpiService.GetMediaQuery().FirstOrDefault(x => x.Id == id);
                    if (model != null)
                    {
                        model.IsClose = true;
                    }
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "已关闭"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpPost]
        public JsonResult Open(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要上线的媒体");
            }
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                foreach (var id in ids)
                {
                    var model = NetMarketingKpiService.GetMediaQuery().FirstOrDefault(x => x.Id == id);
                    if (model != null)
                    {
                        model.IsClose = false;
                    }
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "已开启"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
        #endregion

        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            InjectReportModel(model);
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                var data = NetMarketingKpiService.GetDataQuery().Where(x => x.StoreId == Store.Id).ToList();
                model.MonthDayData = data;
                return View(model);
            }
        }


        public ActionResult MonthTd(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                var config = NetMarketingKpiService.GetMonthConfig(y, m, Store.Id);
                if (config == null)
                {
                    config = new NetMarketingKpiMonthConfig();
                    config.StoreId = Store.Id;
                    config.Month = new DateTime(y, m, 1);
                }
                return View(config);
            }

        }


        public ActionResult InputMonthConfig(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                var config = NetMarketingKpiService.GetMonthConfig(y, m, Store.Id);
                if (config == null)
                {
                    config = new NetMarketingKpiMonthConfig();
                    config.StoreId = Store.Id;
                    config.Month = new DateTime(y, m, 1);
                }
                return View(config);
            }
        }


        public JsonResult ModifyBackMonthConfig(NetMarketingKpiMonthConfig config, List<NetMarketingKpiMediaMonthConfig> mediaconfig, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                var dbconfig = NetMarketingKpiService.GetMonthConfigQuery().FirstOrDefault(x => x.Id == config.Id);
                if (dbconfig == null)
                {
                    dbconfig = new NetMarketingKpiMonthConfig();
                }
                if (isconfirm)
                {
                    dbconfig.IsConfirmed = true;
                }
                if (CurrentUser.Position == Position.SystemManager)
                {
                    dbconfig.StoreId = Store.Id;
                }
                else
                {
                    dbconfig.StoreId = CurrentUser.StoreId.Value;
                }
                dbconfig.Month = config.Month;
                dbconfig.DXMB = config.DXMB;
                dbconfig.SYDXSJLV = config.SYDXSJLV;
                dbconfig.XXZHLVMB = config.XXZHLVMB;
                dbconfig.YDYS = config.YDYS;
                NetMarketingKpiService.SubmitMonthConfig(dbconfig);

                if (mediaconfig == null)
                {
                    mediaconfig = new List<NetMarketingKpiMediaMonthConfig>();
                }
                var list = new List<NetMarketingKpiMediaMonthConfig>();
                foreach (var sel in mediaconfig)
                {
                    sel.Month = config.Month;
                    if (CurrentUser.Position == Position.SystemManager)
                    {
                        sel.StoreId = Store.Id;
                    }
                    else
                    {
                        sel.StoreId = CurrentUser.StoreId.Value;
                    }
                    if (isconfirm)
                    {
                        sel.IsConfirmed = true;
                    }
                    list.Add(sel);
                }
                NetMarketingKpiService.SubmitMediaMonthConfig(config.Month.Year, config.Month.Month, Store.Id, list);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }


        public ActionResult MediaList(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                MediaListModel model = new MediaListModel();
                model.MediaMonthConfigs = NetMarketingKpiService.GetMediaMonthConfigs(y, m, Store.Id).ToList();
                model.Medias = NetMarketingKpiService.GetEnabledMedias(Store.Id).ToList();
                return View(model);
            }
        }

        public ActionResult InputDayData(int y, int m, int d)
        {
            InputDayDataModel model = new InputDayDataModel();
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                model.Date = new DateTime(y, m, d);
                model.DayDatas = NetMarketingKpiService.GetDayDatas(y, m, d, Store.Id).ToList();
                model.Medias = NetMarketingKpiService.GetEnabledMedias(Store.Id).ToList();
                model.IsConfirmed = false;
                if (model.DayDatas.Any(x => x.IsConfirmed))
                {
                    model.IsConfirmed = true;
                }
                return View(model);
            }
        }


        public ActionResult ModifyBackInputDayData(List<NetMarketingKpiDayData> rows, int y, int m, int d, bool isconfirm = false)
        {
            foreach (var row in rows)
            {
                if (isconfirm)
                {
                    row.IsConfirmed = true;
                }
                else
                {
                    row.IsConfirmed = false;
                }
                row.StoreId = Store.Id;
            }
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                NetMarketingKpiService.SubmitDayDatas(y, m, d, Store.Id, rows);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

        //public string MonthTd(int y, int m)
        //{
        //    using (var t = new Transtion())
        //    {
        //        t.Inject(NetMarketingKpiService);
        //        var querysource = NetMarketingKpiService.GetQuery();
        //        querysource = querysource.Where(x => x.StoreId == Store.Id);
        //        var queryresult = querysource.Where(o => o.Month.Year == y && o.Month.Month == m).FirstOrDefault();
        //        if (queryresult == null)
        //        {
        //            return "style='background:Red;padding:10px;'";
        //        }
        //        else if (queryresult.IsConfirmed)
        //        {
        //            return "style='background:Green;padding:10px'";
        //        }
        //        else
        //        {
        //            return "style='background:Yellow;padding:10px;'";
        //        }

        //    }
        //}


        //public ActionResult InputNetMarketingData(int y, int m)
        //{
        //    using (var t = new Transtion())
        //    {
        //        t.Inject(NetMarketingKpiService);
        //        var data = NetMarketingKpiService.GetNetMarketingMonthData(y, m, Store.Id);
        //        if (data == null)
        //        {
        //            data = new Domain.NetMarketingKpi.NetMarketingMonthData();
        //            data.StoreId = Store.Id;
        //            data.Month = new DateTime(y, m, 1);
        //        }
        //        return View(data);
        //    }
        //}


        //public JsonResult ModifyBackInputMonthData(S4Store.Domain.NetMarketingKpi.NetMarketingMonthData config, bool isconfirm = false)
        //{
        //    using (var t = new Transtion())
        //    {
        //        t.Inject(NetMarketingKpiService);
        //        var dbconfig = NetMarketingKpiService.GetQuery().FirstOrDefault(x => x.Id == config.Id);
        //        if (dbconfig == null)
        //        {
        //            dbconfig = new Domain.NetMarketingKpi.NetMarketingMonthData();
        //        }
        //        if (isconfirm)
        //        {
        //            dbconfig.IsConfirmed = true;
        //        }
        //        dbconfig.StoreId = Store.Id;
        //        dbconfig.Month = config.Month;
        //        dbconfig.SYXSKHZHLV = config.SYXSKHZHLV;
        //        dbconfig.DXMB = config.DXMB;
        //        dbconfig.DXPJCJLV = config.DXPJCJLV;
        //        dbconfig.WLYXZJYDYS = config.WLYXZJYDYS;
        //        dbconfig.WLZJSJTRE = config.WLZJSJTRE;
        //        dbconfig.SYWXBGZD = config.SYWXBGZD;
        //        dbconfig.QYWXYDCB = config.QYWXYDCB;
        //        dbconfig.QYWXJHFTS = config.QYWXJHFTS;
        //        dbconfig.QYWXSJFTS = config.QYWXSJFTS;
        //        dbconfig.BYQYWXBGZD = config.BYQYWXBGZD;
        //        dbconfig.SYWBBGZD = config.SYWBBGZD;
        //        dbconfig.QYWBYDCB = config.QYWBYDCB;
        //        dbconfig.QYWBJHFTS = config.QYWBJHFTS;
        //        dbconfig.QYWBSJFTS = config.QYWBSJFTS;
        //        dbconfig.QYWBBGZD = config.QYWBBGZD;
        //        dbconfig.WZ1YDFY = config.WZ1YDFY;
        //        dbconfig.WZ1JHFTS = config.WZ1JHFTS;
        //        dbconfig.WZ1SJFTS = config.WZ1SJFTS;
        //        dbconfig.WZ1HQXXS = config.WZ1HQXXS;
        //        dbconfig.WZ1LDS = config.WZ1LDS;

        //        dbconfig.WZ2YDFY = config.WZ2YDFY;
        //        dbconfig.WZ2JHFTS = config.WZ2JHFTS;
        //        dbconfig.WZ2SJFTS = config.WZ2SJFTS;
        //        dbconfig.WZ2HQXXS = config.WZ2HQXXS;
        //        dbconfig.WZ2LDS = config.WZ2LDS;

        //        dbconfig.WZ3YDFY = config.WZ3YDFY;
        //        dbconfig.WZ3JHFTS = config.WZ3JHFTS;
        //        dbconfig.WZ3SJFTS = config.WZ3SJFTS;
        //        dbconfig.WZ3HQXXS = config.WZ3HQXXS;
        //        dbconfig.WZ3LDS = config.WZ3LDS;

        //        NetMarketingKpiService.SubmitExhibitionWeekData(dbconfig);
        //        t.Commit();
        //        return new JsonResult
        //        {
        //            Data = new
        //            {
        //                success = true
        //            }
        //        };
        //    }
        //}
    }
}
